package com.qi.spzx.manager.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qi.spzx.common.exception.GuiguException;
import com.qi.spzx.manager.mapper.ProductMapper;
import com.qi.spzx.manager.service.*;
import com.qi.spzx.model.dto.product.ProductDto;
import com.qi.spzx.model.entity.product.*;
import com.qi.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/23 18:52
 * @Version 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductSkuService productSkuService;

    @Autowired
    private ProductDetailsService productDetailsService;


    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page,limit);
        List<Product> productList = productMapper.findByPage(productDto);
        //将参数进行处理
        productList.forEach( item ->{
            //设置品牌名称
            Brand brand = brandService.selectById(item.getBrandId());
            if (ObjectUtil.isNotEmpty(brand)){
                item.setBrandName(brand.getName());
            }
            //设置分类名称
            Category category1 = categoryService.selectById(item.getCategory1Id());
            if (ObjectUtil.isNotEmpty(category1)){
                item.setCategory1Name(category1.getName());
            }
            Category category2 = categoryService.selectById(item.getCategory2Id());
            if (ObjectUtil.isNotEmpty(category2)){
                item.setCategory2Name(category2.getName());
            }
            Category category3 = categoryService.selectById(item.getCategory3Id());
            if (ObjectUtil.isNotEmpty(category3)){
                item.setCategory3Name(category3.getName());
            }
            //设置sku集合
            List<ProductSku> productSkuList = productSkuService.selectByProductId(item.getId());
            if (ObjectUtil.isNotEmpty(productSkuList)){
                item.setProductSkuList(productSkuList);
            }
            //设置图片详情列表
            String details = productDetailsService.getDetailsByProductId(item.getId());
            if (StrUtil.isNotEmpty(details)){
                item.setDetailsImageUrls(details);
            }
        });
        return PageInfo.of(productList);
    }

    @Override
    @Transactional
    public void save(Product product) {
        // 保存商品数据
        // 设置上架状态为0
        product.setStatus(0);
        // 设置审核状态为0
        product.setAuditStatus(0);
        productMapper.save(product);

        //进行商品sku数据的保存
        List<ProductSku> productSkuList = product.getProductSkuList();
        for(int i=0,size=productSkuList.size(); i<size; i++) {

            // 获取ProductSku对象
            ProductSku productSku = productSkuList.get(i);
            // 构建skuCode
            productSku.setSkuCode(product.getId() + "_" + i);
            // 设置商品id
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            // 设置销量
            productSku.setSaleNum(0);
            //设置状态
            productSku.setStatus(0);
            // 保存数据
            productSkuService.save(productSku);
        }
        // 保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsService.save(productDetails);
    }

    @Override
    public Product getById(Long id) {
        // 根据id查询商品数据
        Product product = productMapper.selectById(id);
        //将商品的信息进行完善
        //设置品牌名称
        Brand brand = brandService.selectById(product.getBrandId());
        if (ObjectUtil.isNotEmpty(brand)){
            product.setBrandName(brand.getName());
        }
        //设置分类名称
        Category category1 = categoryService.selectById(product.getCategory1Id());
        if (ObjectUtil.isNotEmpty(category1)){
            product.setCategory1Name(category1.getName());
        }
        Category category2 = categoryService.selectById(product.getCategory2Id());
        if (ObjectUtil.isNotEmpty(category2)){
            product.setCategory2Name(category2.getName());
        }
        Category category3 = categoryService.selectById(product.getCategory3Id());
        if (ObjectUtil.isNotEmpty(category3)){
            product.setCategory3Name(category3.getName());
        }

        // 根据商品的id查询sku数据
        List<ProductSku> productSkuList = productSkuService.selectByProductId(id);
        if (ObjectUtil.isNotEmpty(productSkuList)){
            product.setProductSkuList(productSkuList);
        }

        // 根据商品的id查询商品详情数据
        ProductDetails productDetails = productDetailsService.selectByProductId(product.getId());
        if (ObjectUtil.isNotEmpty(productDetails)){
            product.setDetailsImageUrls(productDetails.getImageUrls());
        }

        return product;
    }

    @Override
    @Transactional
    public void updateById(Product product) {
        // 修改商品基本数据
        productMapper.updateById(product);

        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuService.updateById(productSku);
        });

        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsService.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsService.updateById(productDetails);
    }

    @Override
    public void deleteById(Long id) {
        // 根据id删除商品基本数据
        productMapper.deleteById(id);
        // 根据商品id删除商品的sku数据
        productSkuService.deleteByProductId(id);
        // 根据商品的id删除商品的详情数据
        productDetailsService.deleteByProductId(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if(auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}
